<!-- 警情处理页面 -->
<template>
    <div class="page immediateAtten-page" data-page="immediateAtten" style="z-index: 2;">
        <div class="link-page-nav">
            <a href="#" class="link" @click=$root.backToTab()>
                <i class="iconfont icon-zuo"></i>
            </a>
        </div>
        <div class="link-page-top">
            <div class="link-page-title">警情处理</div>
        </div>
        <div class="page-content immediateAtten-content" style="padding-bottom: 100px;padding: 10px 20px 80px;">
            <div class="imm-page" id="chooseType">
                <div class="title">选择操作方法</div> 
                <div class="option">
                    <div class="option-list on" type-id="1">
                        <div class="dot active"></div> 
                        <div class="text">A：领取，但是稍后处理</div>
                    </div>
                    <div class="option-list" type-id="2">
                        <div class="dot"></div> 
                        <div class="text">B：现在就处理</div>
                    </div>
                </div>
            </div>
            <div class="imm-page-center imm-page" id="nowCancel">
                <div class="title">选择处理类型</div>
                <div class="option" id="handlType"></div>
                <div class="title is-ladder-control">是否禁用梯控</div>
                <div class="option is-ladder-control" id="ladder_control"></div>
                <div id="elavator_disabled_time" style="color:red;padding-bottom: 10px; margin-top:-5px;"></div>
                <div class="title">添加备注</div>
                <textarea name="" id="crTb_textarea" class="imm-text cancle-ios-write" placeholder="请输入备注"></textarea>
                <div class="title">添加现场照片</div>
                <div class="imm-swaper" id="immUpload" style="margin: 20px;"></div>
                <br/>
                <div class="title">一并处理其他警情</div>
                <div class="imm-choose-alarm"  id="del-other-police">
                    <input type="text" class="del-other-police-input" placeholder="请选择添加其他警情"  disabled />
                    <input type="text" class="del-other-police-input-other" placeholder="111"  disabled />
                </div>
                <br/>
                <div class="title releve-plan">关联预案</div>
                <div class="imm-choose-alarm releve-plan"  id="select_plan" >
                    <input type="text" placeholder="请选择添加预案" disabled />
                </div>
                <br/>
            </div>
            <div class="releve-plan" style="margin-top: 20px;margin-bottom: 20px;display: none;" id="createEmergencyBtn">
                <label  class="item-checkbox item-content">
                <input checked type="checkbox" id="isToEmegency" name="createEmergency" value="11111"/>
                <i class="icon icon-checkbox" style="display:inline-block;margin-right:12px;"></i>
                <span class="point-details">同步创建应急救援</span>
            </div>
            <div class="imm-page-bottom" @click="quitFireHazardCancel">确定</div>
            <br/>
        </div>
    </div>
</template>
<style>
    .hide{
        display: none;
    }
</style>
<script>
    return {
        mounted() {
            var self = this;
            var $ = self.$$;
            self.recordId = self.$route.params.rId;
            self.recordCreateTime = self.$route.params.rTime;
            self.recordHandlerStatus = self.$route.params.rStatus;
            self.recordOrgId = self.$route.params.rOrg;
            self.fireSTypes = self.$route.params.fireSTypes
            var obj = {};
            obj.id= self.recordId;
            obj.createTime= self.recordCreateTime;
            obj.fcfId = self.$route.params.fcFacilitiesId
            self.policeArr.push(obj);
        },
        data: function () {
            return {
                recordId:"",
                recordCreateTime:"",
                recordHandlerStatus:"",
                recordOrgId:"",
                fireSTypes:"",
                optionType:1,
                dictKey:"",
                picArr:[],
                policeArr:[],
                planId:-1,
                checkTab:0,
                getNewId: (new Date()).getTime(),
                ladderControl:[{name:'不禁用',code:0},{name:'禁用10分钟',code:1},{name:'禁用1小时',code:2},{name:'禁用1天',code:3},{name:'永久禁用',code:4},],
                originHandleForm:{},
                handleForm:{}
            }
        },
        methods: {
            getKeyCancelType:function(){
                var self = this;
                var $ = self.$$;
                $("#handlType").empty();
                Dao.getDict({
                    code:60,
                    userName:userInfor.accountName
                },function(data){
                    if (data.dictValueObj && data.dictValueObj.length > 0) {
                        for (var i = 0; i < data.dictValueObj.length; i++) {
                            $("#handlType").append(`<div class="option-list getKeyCancelType-cancel-type" id=`+data.dictValueObj[i].dictKey+`>
                                <div class="dot"></div> 
                                <div class="text">`+data.dictValueObj[i].dictValue+`</div>
                            </div>`);
                        }
                    }
                    $(".getKeyCancelType-cancel-type").click(function(event) {
                        $("#crTb_textarea").val($(event.currentTarget).children(".text").text());
                        $(".getKeyCancelType-cancel-type").removeClass('on');
                        $(".getKeyCancelType-cancel-type .dot").removeClass("active");
                        $(event.currentTarget).addClass('on');
                        $(event.currentTarget).find(".dot").addClass("active");
                        var dictKey = $(event.currentTarget).attr("id");
                        self.dictKey = dictKey;
                        if(self.dictKey==2){
                            $(".releve-plan").show();
                        }else{
                            $(".releve-plan").hide();
                        }
                    });
                    // 是否显示梯控选项
                    // if(self.fireSTypes==113 || self.fireSTypes == 115){
                    //     $('.is-ladder-control').removeClass('hide')
                    // }else{
                    //     $('.is-ladder-control').addClass('hide')
                    // }

                    // 是否禁用梯控
                    let item = ''
                    for(var i = 0; i < self.ladderControl.length; i++){
                        if(i == 0){
                            item = `<div class="option-list ladder_control_ban" data-ladderControl=`+self.ladderControl[i].code+`>
                                <div class="dot"></div> 
                                <div class="text">`+self.ladderControl[i].name+`</div>
                            </div>`
                        }else{
                            item = `<div class="option-list ladder_control_ban" data-ladderControl=`+self.ladderControl[i].code+`>
                                <div class="dot"></div> 
                                <div class="text">`+self.ladderControl[i].name+`</div>
                            </div>`
                        }
                        $("#ladder_control").append(item);
                    };
                    $("#ladder_control").find('.ladder_control_ban').off('click').on('click',function(event){
                        self.handleClickElevatorStatus($(event.currentTarget))
                    });

                    if(self.fireSTypes==113 || self.fireSTypes == 115){
                        Dao.getFcfAndElecontro({
                            fcFacilitiesId:self.$route.params.fcFacilitiesId
                        },function(data){
                            if(data != null){
                                $('.is-ladder-control').removeClass('hide')
                                self.elevatorStatusInfo=data
                                self.setElevatorStatus(data)
                            }else{
                                $('.is-ladder-control').addClass('hide')
                            }
                        })
                    }else{
                        $('.is-ladder-control').addClass('hide')
                    }
                });
            },
            handleClickElevatorStatus($target){
                var self=this
                $(".ladder_control_ban").removeClass('on');
                $(".ladder_control_ban .dot").removeClass("active");
                $target.addClass('on');
                $target.find(".dot").addClass("active");
                self.checkTab = $target.attr('data-ladderControl')
            },
            setElevatorStatus(data){
                var self=this
                // 禁用状态且没有截止时间为永久禁用
                if(data.status==3){  // 禁用状态
                    self.handleForm.time=''
                    self.handleForm.disableElevator='1'
                    if(!data.disableEndTimeStr){ //永久禁用
                        self.handleForm.time='-1'
                    }
                }else if(data.status==4||data.status==2){ // 正常/触发状态，默认选中不禁用
                    self.handleForm.disableElevator='0'
                    self.handleForm.time=''
                }else{
                    // 不选中是否禁用梯控
                    self.handleForm.disableElevator='-1'
                    self.handleForm.time=''
                }
                if(self.handleForm.disableElevator==='0'){
                    self.handleClickElevatorStatus($("#ladder_control").find('.ladder_control_ban[data-ladderControl="0"]'))
                }else if(self.handleForm.time==='-1'){
                    self.handleClickElevatorStatus($("#ladder_control").find('.ladder_control_ban[data-ladderControl="4"]'))
                }else{
                    $(".ladder_control_ban").removeClass('on');
                    $(".ladder_control_ban .dot").removeClass("active");
                    self.checkTab=''
                }
                // 记录梯控初始状态
                self.originHandleForm.time=self.handleForm.time.toString()
                self.originHandleForm.disableElevator=self.handleForm.disableElevator.toString()
                if(data.status==3&&data.disableStartTimeStr&&data.disableEndTimeStr){
                    $('#elavator_disabled_time').html(`当前梯控禁用时间：<br>${data.disableStartTimeStr} ~ ${data.disableEndTimeStr}`)
                }else if(data.status==3&&(!data.disableStartTimeStr||!data.disableEndTimeStr)){
                    $('#elavator_disabled_time').html('当前梯控状态：永久禁用')
                }else if(data.status==2){
                    $('#elavator_disabled_time').html(`当前梯控状态：触发`)
                }else if(data.status==4){
                    $('#elavator_disabled_time').html(`当前梯控状态：正常`)
                }else if(data.status==5){
                    $('#elavator_disabled_time').html(`当前梯控状态：离线`)
                }
            },
            linkPlan:function(){
                var self = this;
                var $ = self.$$;
                //应急预案
                var planType = [];
                Dao.selectEmergencyPlanInfo({
                    orgIds:self.recordOrgId,
                    pageSize:100,
                    pageIndex:0
                },function(data) {
                    if(data&&data.length>0){
                        for(var i=0;i<data.length;i++){
                            var obj = {};
                            obj.id= data[i].id;
                            obj.name= data[i].name;
                            planType.push(obj);
                        }
                    }  
                });

                let selectOptionIndex = null;
                jQuery("#select_plan").click(function(e) {
                    if(planType.length>0){
                        selectOptionFn(planType,function(data){
                            if(self.planId == data.id){
                                jQuery("#select_plan").removeClass("select-active");
                                jQuery("#select_plan input").val("");
                                self.planId=-1;
                            }else{
                                jQuery("#select_plan input").val(data.name);
                                jQuery("#select_plan").addClass("select-active");
                                self.planId=data.id;
                            }
                            if(selectOptionIndex != data.index){
                                selectOptionIndex = data.index;
                            }else{
                                selectOptionIndex = "";
                            }
                        },selectOptionIndex);
                    }else{
                        app.methods.showToastCenter('暂无相关预案信息'); 
                    }                
                }); 
            },
            //领取任务,但是不处理
            getTask:function(){
                var self = this;
                var $ = self.$$;
                var policeInfo=self.policeArr;
                var info = JSON.stringify(policeInfo).toString()// JsonArray转为String
                common.loading(1);
                let array = []
                let ids = ""
                let arr = JSON.parse(info)
                for(let i in arr){
                    array.push(arr[i].id)
                }
                ids = array.join(",")

                let array1 = []
                let ids1 = ""
                let arr1 = JSON.parse(info)
                for(let i in arr1){
                    array1.push(arr1[i].fcfId)
                }
                ids1 = array1.join(",")
                Dao.handleFireRecord({
                    userName:userInfor.accountName,
                    deviceId:appKeyObj.deviceId,
                    status:2,
                    fireTypeId:0,
                    operator:userInfor.accountName,
                    disableElevator: "-1", // 梯控 不处理传-1 固定值
                    recordsInfosStr:info
                },function(data,info,status){
                    if(status == 200010){
                        app.methods.showToastCenter(info);
                        common.loading(0);
                    }else{
                        app.methods.showToastCenter("领取成功");
                        common.loading(0);
                    }
                    if(JSON.parse(localStorage.getItem("ilink-alarming-push"))){
                        let alarming = JSON.parse(localStorage.getItem("ilink-alarming-push"))
                        alarming.id = ids
                        alarming.handlerStatus = 2
                        localStorage.setItem("ilink-alarming-push",JSON.stringify(alarming))
                    }
                    
                    if(JSON.parse(localStorage.getItem("equipment_material"))){
                        let alarmings = JSON.parse(localStorage.getItem("equipment_material"))
                        alarmings.handleStatus = 2
                        alarmings.fcFacilitiesId = ids1
                        localStorage.setItem("equipment_material",JSON.stringify(alarmings))
                    }
                    
                    app.methods.backToTab();
                    // app.ptr.refresh();
                }); 
            },
            quitFireHazardCancel :function(){
                var self = this;
                var $ = self.$$;
                if(self.optionType==1){
                    self.getTask();
                }else{  
                    self.quitCancelAlarm();
                }
            },
            //立即处理
            quitCancelAlarm:function(){
                var self = this;
                var $ = self.$$;
                var policeInfo=self.policeArr;
                //同步创建应急救援
                var isOpenPlan=0;
                if(jQuery("#isToEmegency").prop("checked")){
                    if(self.dictKey==2){
                        isOpenPlan=1;
                    }else{
                        isOpenPlan=0;
                    }
                }
                var info = JSON.stringify(policeInfo).toString()// JsonArray转为String
                var text = $("#crTb_textarea").val();
                self.picUrls = self.picArr.join(",");
                if(!self.dictKey){
                    app.methods.showToastCenter("请输入选择处理类型");
                    return;
                }
                if(!text){
                    app.methods.showToastCenter("请输入备注信息");
                    return;
                }
                if(self.dictKey==2&&self.planId==-1){
                    app.methods.showToastCenter("请选择预案");
                    return;
                }
                let times = ''
                let disableElevators = -1
                if($('.is-ladder-control').hasClass('hide') == true){
                    self.checkTab = null
                }
                if(self.checkTab == null){
                    times = ''
                    disableElevators = -1
                }else if(self.checkTab == '0'){
                    times = ''
                    disableElevators = 0
                }else if(self.checkTab == '1'){
                    times = '600'
                    disableElevators = 1
                }else if(self.checkTab == '2'){
                    times = '3600'
                    disableElevators = 1
                }else if(self.checkTab == '3'){
                    times = '86400'
                    disableElevators = 1
                }else if(self.checkTab == '4'){
                    times = '-1'
                    disableElevators = 1
                }
                let array = []
                let ids = ""
                let arr = JSON.parse(info)
                for(let i in arr){
                    array.push(arr[i].id)
                }
                ids = array.join(",")

                let array1 = []
                let ids1 = ""
                let arr1 = JSON.parse(info)
                for(let i in arr1){
                    array1.push(arr1[i].fcfId)
                }
                ids1 = array1.join(",")
                var params={
                    userName:userInfor.accountName,
                    deviceId:appKeyObj.deviceId,
                    status:1,
                    fireTypeId:0,
                    time:times,
                    disableElevator:self.isElevatorStatusChanged(times,disableElevators)?disableElevators:'-1',
                    operator:userInfor.accountName,
                    handleTypeId:self.dictKey,
                    remark:text,
                    remarkVoice:"",
                    planId:self.planId==-1?"":self.planId,
                    picUrl:self.picUrls,
                    isOpenPlan:isOpenPlan,//是否创建了应急救援，0未创建，1创建
                    recordsInfosStr:info
                }
                console.log(params)
                // return
                Dao.handleFireRecord(params,function(data,info,status){
                    if(status == 200010){
                        app.methods.showToastCenter(info);
                    }else{
                        app.methods.showToastCenter("处理成功");
                    }
                    if(JSON.parse(localStorage.getItem("ilink-alarming-push"))){
                        let alarming = JSON.parse(localStorage.getItem("ilink-alarming-push"))
                        alarming.handlerStatus = 1
                        alarming.id = ids
                        localStorage.setItem("ilink-alarming-push",JSON.stringify(alarming))
                    }
                    
                    if(JSON.parse(localStorage.getItem("equipment_material"))){
                        let alarmings = JSON.parse(localStorage.getItem("equipment_material"))
                        alarmings.handleStatus = 1
                        alarmings.fcFacilitiesId = ids1
                        localStorage.setItem("equipment_material",JSON.stringify(alarmings))
                    }
                    
                    app.methods.backToTab();
                    // app.ptr.refresh();
                });
            },
            isElevatorStatusChanged(time,disableElevator){
                return time.toString()!==this.originHandleForm.time||disableElevator.toString()!==this.originHandleForm.disableElevator
            }
        },
        on:{
            pageAfterIn: function (e, page) {
                var self = this;
                var $ = self.$$;
                jQuery(".del-other-police-input-other").hide();
                //展示类型
                self.getKeyCancelType();
                $(".option-list").click(function(e){
                    jQuery(".option-list").removeClass("on");
                    jQuery(".option-list .dot").removeClass("active");
                    jQuery(this).addClass("on");
                    jQuery(this).find(".dot").addClass("active");
                    self.optionType = jQuery(this).attr("type-id");
                    // 是否显示梯控选项
                    // debugger
                    // Dao.getFcfAndElecontro({
                    //     fcFacilitiesId:self.$route.params.fcFacilitiesId
                    // },function(data){
                    //     if(data && (self.fireSTypes==113 || self.fireSTypes == 115)){
                    //         $('.is-ladder-control').removeClass('hide')
                    //     }else{
                    //         $('.is-ladder-control').addClass('hide')
                    //     }
                    // })
                    if(self.optionType==1){
                        $("#nowCancel").hide();
                    }else{
                        jQuery(".immediateAtten-content").animate({scrollTop: '150px'}, 400);
                        // 是否禁用梯控
                        $("#ladder_control").empty();
                        let item = ''
                        for(var i = 0; i < self.ladderControl.length; i++){
                            if(i == 0){
                                item = `<div class="option-list ladder_control_ban" data-ladderControl=`+self.ladderControl[i].code+`>
                                    <div class="dot"></div> 
                                    <div class="text">`+self.ladderControl[i].name+`</div>
                                </div>`
                            }else{
                                item = `<div class="option-list ladder_control_ban" data-ladderControl=`+self.ladderControl[i].code+`>
                                    <div class="dot"></div> 
                                    <div class="text">`+self.ladderControl[i].name+`</div>
                                </div>`
                            }
                            $("#ladder_control").append(item);
                        };
                        $("#ladder_control").find('.ladder_control_ban').off('click').on('click',function(event){
                            self.handleClickElevatorStatus($(event.currentTarget))
                        });
                        if(self.elevatorStatusInfo){
                            self.setElevatorStatus(self.elevatorStatusInfo)
                        }
                        $("#nowCancel").show();
                        //初始化上传
                        upLoadImagesCompt(jQuery("#immUpload"),function(pic,isAdd){
                            if(isAdd == "add"){
                                self.picArr.unshift(pic);
                            }else{
                                let index = self.picArr.indexOf(pic);
                                self.picArr.splice(index,1);
                            }
                        },true,null,'immUpload_keyAlarm_container_' + self.getNewId,'immUpload_keyAlarm_pick_' + self.getNewId,'添加现场图片',true);
                    } 
                });
                //一并处理其他警情
                var selectOptionDec=[];
                var selectOptionDecEltype="";
                var dataType="";
                jQuery("#del-other-police").click(function(e) {
                    getNeedDealPolices(self.recordOrgId,function(data){
                        self.policeArr = data.selectedIds;
                        if(self.policeArr.length>1){
                            jQuery(".del-other-police-input").hide();
                            jQuery(".del-other-police-input-other").show();
                            jQuery(".del-other-police-input-other").val('已添加其它'+(self.policeArr.length-1)+'条警情');
                        }else{
                            jQuery(".del-other-police-input-other").hide();
                            jQuery(".del-other-police-input").show();
                        }
                        var isHave=true;//判断是否包含起本身警情
                        for(var i=0;i<self.policeArr.length;i++){
                            if(self.policeArr[i].id==self.recordId){
                                isHave=false;
                            }
                        }
                        if(isHave){//不包含的情况
                            var obj = {};
                            obj.id= self.recordId;
                            obj.createTime= self.recordCreateTime;
                            self.policeArr.push(obj);
                        }
                    },false,self.policeArr,selectOptionDecEltype,dataType,self.recordId);
                });

                //关联预案
                self.linkPlan();

                //已经被领取
                if(self.recordHandlerStatus==2){
                    $("#nowCancel").show();
                    $("#chooseType").hide();
                    self.optionType = 2;
                    //初始化上传
                    upLoadImagesCompt(jQuery("#immUpload"),function(pic,isAdd){
                        if(isAdd == "add"){
                            self.picArr.unshift(pic);
                        }else{
                            let index = self.picArr.indexOf(pic);
                            self.picArr.splice(index,1);
                        }
                    },true,null,'immUpload_keyAlarm_container','immUpload_keyAlarm_pick','添加现场图片',true);
                }else{
                    $("#nowCancel").hide();
                    $("#chooseType").show();
                    self.optionType = 1;
                }           
            },
        }
    }
</script>